var util = require("../../../utils/util.js");
var api = require('../../../config/api.js');
Page({
  data: {
    subjectOne: [],
    subjectTwo: [],
    subjectThree: [],
    subjectFour: [],
    activeCategoryId: 3,
    activeTabId: 1,
    redList: [],
    blackList: [],
    monthText:'',
    entity:'',
    newslist:[],
    type:'',
    yearMonth:'',
    jlyearMonth:'',
    schoolList:[],
    coachs:[],
    page: 1,
    limit: 30,
    totalPages: 1,
    keyword: '',
    schoolId: 0,
    regionId: 0,
    coachlist:[]
  },
  onLoad: function () {
    this.loadData();
    // this.loadData_Jl();
    this.getSchoolStar();
    let that = this;
    /*util.request(api.newsDetailById, {
      id: 'f4b9c2e6-84ca-4874-ac60-600deb214c09'
    }).then(function (res) {
      console.log(res.data);
      that.setData({
        entity: res.data
      });
    });*/

    that.setData({
      type:'4'
    });
    //console.log('vvvvvv=-002');
    util.request(api.queryNewsAll,{type:'4'}).then(function (res) {
      console.log(res.data);
      that.setData({
        newslist:res.data.list
      });
    });



  },
  // onShow: function () {
  //   var that = this;
  //   this.setData({
  //     page: 1,
  //     coachs: []
  //   });
  //   that.getCoachs();
  // },
  // loadPageData() {
  //   //console.log('vvvvvvvvvvvvvysxm');
  //   if (this.data.totalPages > this.data.page) {
  //     this.setData({
  //       page: this.data.page + 1
  //     });
  //     this.getCoachs();
  //   } else {
  //     wx.showToast({
  //       title: '没有更多数据了',
  //       icon: 'none',
  //       duration: 2000
  //     });
  //     return false;
  //   }
  // },
  // getCoachs() {
  //   let that = this;
  //   if(that.data.page == 1){
  //     that.setData({
  //       coachs: []
  //     });
  //   }
    
  //   wx.showLoading({
  //     title: '加载中...',
  //   });

  //   util.request(api.listStarUrl, {
  //     keyword: that.data.keyword,
  //     schoolId: that.data.schoolId,
  //     regionId: that.data.regionId,
  //     starRating: '0',
  //     page: that.data.page,
  //     limit: that.data.limit,
  //     sort: "satisfaction+0"
  //   }).then(function (res) {
  //     //console.log('vvvvvvvvvvvvvsssssssssssv:'+JSON.stringify(res));
  //     if (res.errno === 0) {
  //       that.setData({
  //         coachs: that.data.coachs.concat(res.data.list),
  //         totalPages: res.data.pages
  //       });
  //     }

  //     wx.hideLoading();
  //   });
  // },
  getSchoolStar:function(){
    var that = this
    util.request(api.schoolStarUrl,{}).then(function (res) {
      //console.log(JSON.stringify(res.data));
      that.setData({
        schoolList:res.data
      })
    });
  },
  toNewsRead:function(e){
    var param = e.currentTarget.dataset;
    var id = param["id"];
    //console.log(id);
    wx.navigateTo({
      url: '../../news/read/read?id=' + id
    })
  },
  tabClick: function (e) {

  },
  loadData_Jl: function () {
    var that = this;
    util.request(api.ExampleCoach, {}).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          redList: res.data.redList,
          blackList: res.data.blackList
        });
      }
    });
  },
  jlreducemonth:function(){
    var that = this;
    var months = this.addMonths(this.data.jlyearMonth,-1);
    that.setData({
      jlyearMonth:months
    });
    this.loadDataJl();
  },
  jlnextmonth:function(){
    var that = this;
    var months =  this.addMonths(this.data.jlyearMonth,1);
    that.setData({
      jlyearMonth:months
    });
    this.loadDataJl();
  },
  reducemonth:function(){
    var that = this;
    var months = this.addMonths(this.data.yearMonth,-1);
    that.setData({
      yearMonth:months
    });
    this.loadDataDetai();
  },
  nextmonth:function(){
    var that = this;
    var months =  this.addMonths(this.data.yearMonth,1);
    that.setData({
      yearMonth:months
    });
    this.loadDataDetai();
  },
  loadData: function () {
    var that = this;
    let yy = new Date().getFullYear()
    let mm = new Date().getMonth()
    if(mm == 0){
      mm = 12;
      yy = yy-1;
    }
    //console.log(mm);
    if(mm < 10){
      mm = '0'+mm;
    }
    that.setData({
      yearMonth:yy+'-'+mm,
      jlyearMonth:yy+'-'+mm
    });
    this.loadDataDetai();
    this.loadDataJl();
  },
  loadDataJl(){
    var that = this;
    util.request(api.queryCoachMonthSuggUrl, {month:this.data.jlyearMonth}).then(function (res) {
      // console.log(JSON.stringify(res));
      that.setData({
        coachlist: res.data
      })
    })
  },
  loadDataDetai(){
    var that = this;
    util.request(api.ExampleSchool, {yearMonth:this.data.yearMonth}).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          subjectOne: res.data.subject1,
          subjectTwo: res.data.subject2,
          subjectThree: res.data.subject3,
          subjectFour: res.data.subject4
        });
      }
    });
  },
  getMonth:function(val){
    //var val = '2021-12-03 20:09:49';
    var date = new Date(val);
    var month = date.getMonth();
    console.log(month);
    var that = this;
    if(month === 1){that.setData({monthText:'一月份'})}
    if(month === 2){that.setData({monthText:'二月份'})}
    if(month === 3){that.setData({monthText:'三月份'})}
    if(month === 4){that.setData({monthText:'四月份'})}
    if(month === 5){that.setData({monthText:'五月份'})}
    if(month === 6){that.setData({monthText:'六月份'})}
    if(month === 7){that.setData({monthText:'七月份'})}
    if(month === 8){that.setData({monthText:'八月份'})}
    if(month === 9){that.setData({monthText:'九月份'})}
    if(month === 10){that.setData({monthText:'十月份'})}
    if(month === 11){that.setData({monthText:'十一月份'})}
    if(month === 0){that.setData({monthText:'十二月份'})}
  },
  tabClick: function (e) {
    this.setData({
      //activeIndex: e.currentTarget.id
      activeCategoryId: e.currentTarget.id
    });
  },
  // 滑动底部加载
  lower: function () {
  },

  onReachBottom: function () {
  },
  addMonths(yearMonthDay,monthNum){
    var arr=yearMonthDay.split('-');//2020-08-19或2020-08
    var year=parseInt(arr[0]);
    var month=parseInt(arr[1]);
    month=month+monthNum;
    if(month>12){//月份加
        var yearNum=parseInt((month-1)/12);
        month=month%12==0?12:month%12;
        year+=yearNum;
    }else if(month<=0){//月份减
        //month=Math.abs(month);
        //var month=parseInt((month+12)/12);
        month = 12;
        year=year-1;
    }
    month=month<10?"0"+month:month;
    return year+"-"+month;
  }
})